<?php
/**
 * 后台初始化控制器类
 * @author goss,<goss@lunluoren.com>
 */
abstract class AdministratorAction extends Action {
	
	protected $modelName  = null;
	protected $modelObj   = null;
	const PUBLISH_FIELD   = 'publish';
	const RECOMMEND_FIELD = 'recommend';
	
	/**
	 * 初始化数据
	 */
	public function _initialize(){
		$this->checkLogin();
		$this->modelName = ucfirst($this->modelName);
		$this->modelObj = D($this->modelName);
		$this->assign('siteName',SITE_NAME);
	}
	
	/**
	 * 添加或修改数据
	 */	
	public function edit(){
		$id = $this->_request('id');
		if ( $id ){
			$data = $this->modelObj->where($this->modelObj->getPk().'='.$id)->find();
		}
		try {
			if ( $this->isPost() ){
				$this->modelObj->create();
				if ( $id && $data ){
					$this->modelObj->where($this->modelObj->getPk().'='.$id)->save();
				}
				else{
					$this->modelObj->add();
				}
				$this->success('保存成功!');
			}
			else{
				$this->assign('data',$data);
				$this->display();
			}
		} catch (ThinkException $e) {
			$this->error($e->getMessage());
		}
	}
	
	/**
	 * 删除数据
	 */
	public function delete(){
		try {
			$ids = $this->_get('id');
			if ( !$ids && $this->isPost() ){
				$ids = $_POST['id'];
			}
			if ( is_array($ids) ){
				$idstr = implode(',', $ids);
				$count = $this->modelObj->where($this->modelObj->getPk()." in($idstr)")->delete();
			}
			elseif(is_numeric($ids)){
				$count = $this->modelObj->where($this->modelObj->getPk()." = $ids")->delete();
			}
			else{
				$this->error('没有选择任何数据');
			}
			$this->success("成功删除$count条记录");
		} catch (ThinkException $e) {
			$this->error($e->getMessage());
		}
	}
	
	/**
	 * 发布
	 */
	public function publish(){
		try {
			$id = $this->_get('id') ? $this->_get('id') : $_POST['id'];
			if ( is_array($id) ){
				$where[$this->modelObj->getPk()] = array('IN',implode(',', $id));
			}
			else{
				$where[$this->modelObj->getPk()] = array('EQ',$id);
			}
			$this->modelObj->where($where)->setField(self::PUBLISH_FIELD,1);
		} catch (ThinkException $e) {
			$this->error($e->getMessage());
		}
		$this->success('操作成功!');
	}
	
	/**
	 * 停止发布
	 */
	public function stopPublish(){
		try {
			$id = $this->_get('id') ? $this->_get('id') : $_POST['id'];
			if ( is_array($id) ){
				$where[$this->modelObj->getPk()] = array('IN',implode(',', $id));
			}
			else{
				$where[$this->modelObj->getPk()] = array('EQ',$id);
			}
			$this->modelObj->where($where)->setField(self::PUBLISH_FIELD,0);
		} catch (ThinkException $e) {
			$this->error($e->getMessage());
		}
		$this->success('操作成功!');
	}
	
	/**
	 * 推荐
	 */
	public function recommend(){
		$id = $this->_get('id') ? $this->_get('id') : $_POST['id'];
		try {
			if ( is_array($id) ){
				$where[$this->modelObj->getPk()] = array('IN',implode(',', $id));
			}
			else{
				$where[$this->modelObj->getPk()] = array('EQ',$id);
			}
			$this->modelObj->where($where)->setField(self::RECOMMEND_FIELD,1);
		} catch (ThinkException $e) {
			$this->error($e->getMessage());
		}
		$this->success('操作成功!');
	}
	
	/**
	 * 取消推荐
	 */
	public function stopRecommend(){
		try {
			$id = $this->_get('id') ? $this->_get('id') : $_POST['id'];
			if ( is_array($id) ){
				$where[$this->modelObj->getPk()] = array('IN',implode(',', $id));
			}
			else{
				$where[$this->modelObj->getPk()] = array('EQ',$id);
			}
			$this->modelObj->where($where)->setField(self::RECOMMEND_FIELD,0);
		} catch (ThinkException $e) {
			$this->error($e->getMessage());
		}
		$this->success('操作成功!');
	}
	
	protected function checkLogin(){}
	
	protected function _checkFormData(){}
}